{extend name="base"/}
{block name="resources"}
<link rel="stylesheet" type="text/css" href="SHOP_CSS/message.css" />
{/block}
{block name="main"}
<!-- 买家消息 -->
<div class="layui-card ns-card-common ns-card-brief">
	<div class="layui-card-header">
		<span class="ns-card-title">买家消息</span>
	</div>
	<div class="layui-card-body layui-field-box">
		{foreach $member_message_list as $member_message_k => $member_message_v}
		<div class="block-list">
			<div class="block-title">
				{$member_message_v.title}
				{php}
					$sms_json = json_decode($member_message_v['sms_json'],true);
					$wechat_json = json_decode($member_message_v['wechat_json'],true);
				{/php}
			</div>
			<div class="block-content">

				{if in_array('sms',$member_message_v['support_type'])}
				<a class="note layui-form">
					<input type="checkbox" name="" value="1" title="短信" lay-filter="sms_is_open" lay-skin="primary" {if isset($member_message_v.sms_is_open) && $member_message_v.sms_is_open == 1}checked{/if}>
					<input type="hidden" name="keywords" value="{$member_message_v.keywords}" />

					<div class="sms-hide">
						<p>短信内容：{$member_message_v['sms_content'] != '' ? $member_message_v['sms_content'] : '平台尚未配置'}</p>
					</div>

				</a>
				{/if}

				{if in_array('wechat',$member_message_v['support_type'])}
				<a class="note layui-form">
					<input type="checkbox" name="" value="1" title="微信公众号" lay-filter="wechat_is_open" lay-skin="primary" {if isset($member_message_v.wechat_is_open) && $member_message_v.wechat_is_open == 1}checked{/if}>
					<input type="hidden" name="keywords" value="{$member_message_v.keywords}" />
					{if condition="$wechat_json"}
					<div class="sms-hide">
						<p>模板id：{$wechat_json['template_id']}</p>
						<pre>模板内容：{$wechat_json['content']}</pre>
					</div>
					{/if}
				</a>
				{/if}
			</div>
		</div>
		{/foreach}
	</div>
</div>

<!-- 卖家通知 -->
<div class="layui-card ns-card-common ns-card-brief">
	<div class="layui-card-header">
		<span class="ns-card-title">卖家通知<a style="margin-left:20px" href="{:addon_url('shop/shopacceptmessage/lists')}">商家会员管理（用以接受消息通知）</a></span>
	</div>
	<div class="layui-card-body layui-field-box">
		{foreach $shop_message_list as $shop_message_k => $shop_message_v}
		<div class="block-list">
			<div class="block-title">
				{$shop_message_v.title}
				{php}
				$sms_json = json_decode($shop_message_v['sms_json'],true);
				$wechat_json = json_decode($shop_message_v['wechat_json'],true);
				{/php}
			</div>
			<div class="block-content">
				{if in_array('sms',$shop_message_v['support_type'])}
				<a class="note layui-form">
					<input type="checkbox" name="" title="短信" lay-skin="primary" lay-filter="sms_is_open" {if isset($shop_message_v.sms_is_open) && $shop_message_v.sms_is_open == 1}checked{/if}>
					<input type="hidden" name="keywords" value="{$shop_message_v.keywords}" />
					<div class="sms-hide">
						<p>短信内容：{$shop_message_v['sms_content'] != '' ? $shop_message_v['sms_content'] : '平台尚未配置'}</p>
					</div>
				</a>
				{/if}

				{if in_array('wechat',$shop_message_v['support_type'])}
				<a class="note layui-form">
					<input type="checkbox" name="" title="微信公众号" lay-skin="primary" lay-filter="wechat_is_open" {if isset($shop_message_v.wechat_is_open) && $shop_message_v.wechat_is_open == 1}checked{/if}>
					<input type="hidden" name="keywords" value="{$shop_message_v.keywords}" />
					{if condition="$wechat_json"}
					<div class="sms-hide">
						<p>模板id：{$wechat_json['template_id'] ?? ''}</p>
						<p>模板内容：{$wechat_json['content'] ?? ''}</p>
					</div>
					{/if}
				</a>
				{/if}
			</div>
		</div>
		{/foreach}
	</div>
</div>

{/block}
{block name="script"}
<script>
	layui.use('form', function() {
		var form = layui.form,
			repeat_flag = false; //防重复标识

		//短信
			form.on('checkbox(sms_is_open)', function(data) {


			var sms_is_open = 0;
			if (data.elem.checked) {
				sms_is_open = 1;
			}
			
			var keywords = $(data.elem).siblings("input[name='keywords']").val();

			if (repeat_flag) return;
			repeat_flag = true;

			$.ajax({
				dataType: 'JSON',
				type: 'POST',
				url: ns.url("shop/message/edit"),
				data: {
					"sms_is_open": sms_is_open,
					"keywords": keywords
				},
				success: function(res){
					repeat_flag = false;
					if(res.code >= 0){
						layer.msg(res.message);
					}else {
						layer.msg('配置失败，平台尚未开启该模板');
						$(data.elem).prop('checked',false);
						form.render();
					}

				}
			});
		});

		//微信公众号
        form.on('checkbox(wechat_is_open)', function(data) {
            var wechat_is_open = 0;
            if (data.elem.checked) {
                wechat_is_open = 1;
            }

            var keywords = $(data.elem).siblings("input[name='keywords']").val();

            if (repeat_flag) return;
            repeat_flag = true;

            $.ajax({
                dataType: 'JSON',
                type: 'POST',
                url: ns.url("wechat://shop/message/edit"),
                data: {
                    "wechat_is_open": wechat_is_open,
                    "keywords": keywords
                },
                success: function(res){
                    repeat_flag = false;
                    layer.msg(res.message);

                    // if (res.code == 0) {
                    //     location.reload();
                    // }
                }
            });
        });
	})
</script>
{/block}
